/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package alloy.ast;

import java.util.Iterator;

public class QualifiedModuleName extends QualifiedName{
	
	/**
	 * index of type parameters in child list
	 */
	private static final int TYPE_PARAMS_INDEX = 2;
	
	public QualifiedModuleName(Location location_, Id id_, Path path_, QualifiedNames typeParams_) {
		super (location_, id_, path_);
		addChild(typeParams_);
	}
	
	public QualifiedModuleName(Id id_, Path path_, QualifiedNames typeParams_) {
		super (id_, path_);
		addChild(typeParams_);
	}
	
	/**
	 * gets the type parameters of this
	 * @return the {@link QualifiedNames}
	 */
	public QualifiedNames getTypeParams() {
		return (QualifiedNames)childAt(TYPE_PARAMS_INDEX);
	}
	
	/**
	 * sets the type parameters of this
	 */
	public void setTypeParams(QualifiedNames newTypeParams_) {
		setChild(TYPE_PARAMS_INDEX, newTypeParams_);	
	}
	
	/**
	 * @return true if this has no type parameters
	 */
	public boolean hasNoTypeParams() {
		return !(getTypeParams().getQualifiedNameIter().hasNext());	
	}
	/**
	 * @param obj the object for comparison
	 * @return <pre>true</pre> if this name is the same as obj; <pre>false</pre> otherwise
	 */
	public boolean equals(Object obj) {
		if (!super.equals(obj)) {
			return false;
		}
		if (obj instanceof QualifiedModuleName) {
			QualifiedModuleName other = (QualifiedModuleName)obj;

			if (numChildren() != other.numChildren()) {
				return false;
			}
			
			Iterator i = getTypeParams().getQualifiedNameIter();
			Iterator j = other.getTypeParams().getQualifiedNameIter();
			
			// iterate and compare
			while (i.hasNext()) {
				if (!i.next().equals(j.next())) {
					return false;
				}
			}
			return true;
		}
		return false;
	}
	
	public int hashCode() {
		int paramHash = 0;
		for (Iterator i = getTypeParams().getQualifiedNameIter(); i.hasNext();) {
			paramHash += i.next().hashCode();
		}
		return getId().hashCode() + getPath().hashCode() + paramHash;
	}
	
	public String unparameterizedNodeString() {
		return super.nodeString();
	}
	
	public String nodeString() {
		if (hasNoTypeParams()) {
			return super.nodeString();
		} else {
			String typeParamsString = "";
			for (Iterator i = getTypeParams().getQualifiedNameIter(); i.hasNext();) {
				typeParamsString += i.next().toString();
				if (i.hasNext()) {
					typeParamsString += ",";
				}
			}
			
			return super.nodeString() + "[" + typeParamsString + "]";
		}
	}
}

